<?php
/**
 * Created by PhpStorm.
 * Login: 曹旺
 * Date: 2018-03-08
 * Time: 17:45
 */

namespace app\index\model;

use think\Cache;
use think\Model;
use think\Db;

class User extends Model
{
    public function userAdress()
    {
        return $this->hasOne('UserAddress', 'user_id', 'id')->field('nickname');
    }

    /**
     * @param $arr账号密码
     * @return mixed
     * 登录数据
     * @author  曹旺
     * @data  13:23
     */
    public function user_login($arr)
    {
        //用户的id和昵称
        $arrw = model('user')->field('id,nickname')->where($arr)->where(['state' => 1])->select()->toArray();
        if (count($arrw) > 1) {
            model('user')->where($arr)->update(['state' => 0]);
            //return 123;
        }
        return $arrw;
    }

    /**
     * @param $arr用户ID
     * @return array
     * 个人信息展示
     * @author  曹旺
     * @data  13:43
     */
    public function user_myselfinfo($arr)
    {
        $res = Db::table('nbsm_user')->field('nickname,sex,birthday')->where($arr)->find();
        return $res;
    }

    /**
     * @param $id用户ID
     * @return mixed
     * 个人主页
     * @author  曹旺
     * @data  09:42
     */
    public function myselfs($id)
    {
        //用户的id和昵称
        $arrw = model('user')->field('id,nickname,head_portrait')->where(['id' => $id])->find()->toArray();
        //待付款的订单数
        $ending_payment_number = Db::table('nbsm_order')->where(['user_id' => $arrw['id'], 'state_del' => 1, 'order_state' => 1])
            ->count();
        $ending_payment = ['number' => $ending_payment_number, 'state' => 1];
        //待发货
        $pending_delivery_number = Db::table('nbsm_order')->where(['user_id' => $arrw['id'], 'state_del' => 1, 'order_state' => 2])
            ->count();
        $pending_delivery = ['number' => $pending_delivery_number, 'state' => 2];
        //待收货
        $goods_to_be_received_number = Db::table('nbsm_order')->where(['user_id' => $arrw['id'], 'state_del' => 1, 'order_state' => 3])
            ->count();
        $goods_to_be_received = ['number' => $goods_to_be_received_number, 'state' => 3];
        //待评价 暂时还没有
        /*$to_be_evaluated_number = Db::table('nbsm_order')->where(['user_id' => $arrw['id'], 'state_del' => 1, 'order_state' => 4])
            ->count();
        $to_be_evaluated = ['number' => $to_be_evaluated_number, 'state' => 4];*/
        $result['userinfo'] = $arrw;
        $result['ending_payment'] = $ending_payment;
        $result['pending_delivery'] = $pending_delivery;
        $result['goods_to_be_received'] = $goods_to_be_received;
        //$result['to_be_evaluated'] = $to_be_evaluated;
        return $result;
    }

    /**
     * @param $data
     * data  数组  [ 手机号 密码  验证码（目前还没加）]
     * @return int|string
     * 返回0失败 1成功 2手机号被注册
     * @author  曹旺
     * @data  14:07
     */
    public function user_register($data)
    {
        $rand=Cache::get($data['phone'].$data['type'].'rand');
        //$rand_arr=json_decode($rand,true);
        //return $rand['code'];
        if (isset($rand) && $rand['code']==$data['rand']){
            //return 1;
            $res = Db::table('nbsm_user')->where(['phone' => $data['phone']])->select();
            if (!empty($res)) {
                return 2;
            } else {
                //return 10;
                $arr['password'] = md5($data['password']);
                $arr['nickname'] = $data['phone'];
                $arr['phone']=$data['phone'];
                $result = Db::table('nbsm_user')->insert($arr);
                return $result;
            }
        }else{
            return 3;
        }

    }

    /**
     * @param $arr用户修改的内容
     * @return int|string
     * 个人信息修改的数据处理
     * @author  曹旺
     * @data  16:19
     */
    public function user_myselfinfo_select($arr)
    {
        $res = Db::table('nbsm_user')->update($arr);
        return $res;
    }

    /**
     * @param $data [用户id 邮箱 验证码类型 验证码 新密码]
     * @return int|string
     *  通过邮箱修改密码
     * @author  曹旺
     * @data  11:10
     */
    public function myselfinfo_save_pass_e($data)
    {
        //return $data['password'];
        $rand = Cache::get($data['email'] . $data['type'] . 'rand');
        //return $rand;
        //$rand=Cache::get($data['email'].$data['type'].'rand');
        if ($rand == $data['rand']) {
            //$date['id'] = $data['id'];
            $dates['email'] = $data['email'];
            $date['email'] = $data['email'];
            $date['password'] = md5($data['password']);
            $str = Db::table('nbsm_user')->where($date)->find();
            if (!empty($str)) {
                //新旧密码一致
                return 2;
            } else {
                //判断当前用户是否绑定的这个邮箱
                $as = Db::table('nbsm_user')->where(['email' => $data['email']])->find();
                if ($as) {
                    $arr = Db::table('nbsm_user')->where($dates)->update($date);
                    return $arr;
                } else {
                    return 0;
                }
            }
        } else {
            return 3;
        }
    }


    /**
     * @param $data [用户id 手机号 验证码类型 验证码 新密码]
     * @return int|string
     *  通过邮箱修改密码
     * @author  曹旺
     * @data  11:16
     */
    public function myselfinfo_save_pass_p($data)
    {
        $rand = Cache::get($data['phone'] . $data['type'] . 'rand');
        //return $rand;
        //$rand=Cache::get($data['email'].$data['type'].'rand');
        if ($rand['code'] == $data['rand']) {
            $date['phone'] = $data['phone'];
            $date['password'] = md5($data['password']);
            $str = Db::table('nbsm_user')->where($date)->find();
            if (!empty($str)) {
                //新旧密码一致
                return 2;
            } else {
                $arr = Db::table('nbsm_user')->where(['phone'=>$data['phone']])->update($date);
                return $arr;
            }

        } else {
            return 3;
        }
    }

    /**
     * @param $id用户ID
     * @param $data [email或phone]
     * @return int
     * 判断用户的邮箱或手机是否与账号信息的一致
     * @author  曹旺
     * @data  13:19
     */
    public function is_user($id, $data)
    {
        $res = Db::table('nbsm_user')->where(['id' => $id])->where($data)->find();
        if (!empty($res)) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * @param $data[验证码类型，用户id，邮箱，验证码]
     * @return int|string
     * 邮箱绑定
     * @author  曹旺
     * @data  15:03
     */
    public function myselfinfo_binding_e($data){
        $rand = Cache::get($data['email'] . $data['type'] . 'rand');
        //return $rand;
        //$rand=Cache::get($data['email'].$data['type'].'rand');
        if ($rand == $data['rand']) {
            $date['id'] = $data['id'];
            $date['email'] = $data['email'];
            //$date['password'] = md5($data['password']);
            $str = Db::table('nbsm_user')->where($date)->find();
            if (!empty($str)) {
                //新旧邮箱一致
                return 2;
            } else {
                $arr = Db::table('nbsm_user')->update($date);
                return $arr;
            }
        } else {
            return 3;
        }
    }

    /**
     * @param $data[验证码类型，用户id，手机号，验证码]
     * @return int|string
     * 手机绑定
     * @author  曹旺
     * @data  15:02
     */
    public function myselfinfo_binding_p($data){
        $rand = Cache::get($data['phone'] . $data['type'] . 'rand');
        //return $rand;
        //$rand=Cache::get($data['email'].$data['type'].'rand');
        if ($rand['code'] == $data['rand']) {
            $date['id'] = $data['id'];
            $date['phone']=$data['phone'];
            //$date['password'] = md5($data['password']);
            $str = Db::table('nbsm_user')->where($date)->find();
            if (!empty($str)) {
                //新旧密码一致
                return 2;
            } else {
                $arr = Db::table('nbsm_user')->update($date);
                return $arr;
            }
        } else {
            return 3;
        }
    }
}